package com.yinhaochuan.result;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PrintTopFile {

	static Connection connection;

	static {
		connection = setConnection("fi");
	}

	public static Connection setConnection(String database) {
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/" + database;
		String user = "root";
		String password = "123456";
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);
			if (!conn.isClosed()) {
				System.out.println("Succeeded connectiong to the Database :"
						+ database);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public String createTableName(int queryID, String prefix) {
		String table = prefix + "_query_";
		String _name = "";
		if (queryID < 10) {
			_name = "00" + queryID;

		} else {
			_name = "0" + queryID;
		}
		String tablename = table + _name;
		return tablename;
	}

	public void printQuery(String output,String prefix) throws IOException, SQLException {
		PrintWriter pw = new PrintWriter(new FileOutputStream(
				output));
		StringBuffer sb = new StringBuffer(1024 * 1024 * 3);
		String queryID = null;
		for (int i = 1; i <= 50; i++) {
			System.out.println(i);
			 if (i < 10) {
				 queryID = "00" + i;
				
			} else {
				queryID = "0" +i;
			}
			String tablename = createTableName(i, prefix);
			String sql = "select * from " + tablename + " order by rate28 DESC";
			System.out.println(sql);
			try {
				PreparedStatement ps = connection.prepareStatement(sql);
				ResultSet rs = ps.executeQuery();
				int flag = 0;
				int id = 1;
				while (rs.next()) {
					if (1000 == flag) {
						System.out.println(tablename + " output succeeded");
						break;
					}
					String docname = rs.getString(1);
					Double rate28 = rs.getDouble(2);
					sb.append(queryID + "\t0\t" + docname + "\t" + id + '\t'
							+ rate28 + "\t" + "CCNU-C-C-T-01" + "\n");
					id++;
					flag++;
				}
				if (rs!=null) {
					rs.close();
				}
				if (ps!=null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		pw.println(sb.toString());
		pw.flush();
		pw.close();
	}

	public static void main(String[] args) throws IOException, SQLException {
		
		String output1 = "./data/results/single_title_result.txt";
		new PrintTopFile().printQuery(output1,"title_");
		
		output1 = "./data/results/single_desc_result.txt";
		new PrintTopFile().printQuery(output1, "desc_");
		
		String output2 = "./data/results/order_title_result.txt";
		new PrintTopFile().printQuery(output2,"order_title_");
		
		output2 = "./data/results/order_desc_result.txt";
		new PrintTopFile().printQuery(output2, "order_desc_");
		
		String output3 = "./data/results/unorder_title_result.txt";
		new PrintTopFile().printQuery(output3,"unorder_title_");
		
		output3 = "./data/results/unorder_desc_result.txt";
		new PrintTopFile().printQuery(output3, "unorder_desc_");
	}

}
